﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class LoginController : Controller
    {
        private SchoolEntities _context;
        public LoginController()
        {
            _context = new SchoolEntities();
        }
        // GET: Login
        public ActionResult Index()
        {
            string str = Session["username"].ToString();
            ViewBag.User = str;
            return View();
        }

        public ActionResult Login()
        {

            return View();
        }

        [HttpPost]
        public ActionResult Login(string username, string pwd)
        {
            var users = _context.Users.FirstOrDefault(x => x.Username == username);
            if (users == null)
            {
                ViewBag.Msg = "用户名错误";
            }
            else
            {
                if (users.Pwd == pwd)
                {
                    Session["username"] = users.Username;
                    return RedirectToAction("Index", "Default");
                }
                else
                {
                    ViewBag.Msg = "用户名或密码错误";
                }
            }
            ViewBag.user = users;
            return View();
        }
    }
}